package com.zhouz.admin.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhouz.admin.app.annotation.RequiresPermissionsDesc;
import com.zhouz.admin.app.db.domain.OptLog;
import com.zhouz.admin.app.db.domain.OptRequestContext;
import com.zhouz.admin.app.db.service.OptLogService;
import com.zhouz.admin.app.db.service.OptRequestContextService;
import com.zhouz.admin.app.db.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张鹏
 * @since 2018-11-20
 */
@RestController
@RequestMapping("/admin/optLog")
public class AdminOptLogController {
    @Autowired
    private OptLogService logService;
    @Autowired
    private OptRequestContextService requestContextService;

    @GetMapping("/list")
    @RequiresPermissions("admin:optLog:list")
    @RequiresPermissionsDesc(menu={"系统管理" , "系统日志"}, button="查询")
    public Result page(String key , String businessNo, String exception, Integer page , Integer limit, String app, String userName){

        QueryWrapper<OptLog> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(businessNo)){
            wrapper.eq(OptLog.BUSINESS_NO,businessNo);
        }
        if(StringUtils.isNotBlank(exception)){
            wrapper.eq(OptLog.IS_EXCEPTION,exception);
        }
        if(StringUtils.isNotBlank(app)){
            wrapper.eq("app",app);
        }
        if(StringUtils.isNotBlank(userName)){
            wrapper.eq("user_name",userName);
        }
        if(StringUtils.isNotBlank(key)){
            wrapper.and(i -> i.like(OptLog.URL,key));
            wrapper.or(i -> i.like(OptLog.URL_DESC,key));
        }
        IPage<OptLog> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        wrapper.orderByDesc(OptLog.ID);
        pages = logService.page(pages,wrapper);
        return Result.success(pages);
    }

    @GetMapping("getInfo")
    public Result<OptRequestContext> getInfo(String requestNo){
        if(StringUtils.isBlank(requestNo)){
            return Result.badArgumentValue().msg("code为不能空");
        }
        OptRequestContext detail = requestContextService.getOne(new QueryWrapper<OptRequestContext>().eq(OptRequestContext.REQUEST_NO,requestNo));
        return Result.success(detail);
    }

}

